package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.AnnualVeriRepEndDate;
import com.ldj.hibernate.AnnualVeriRepEndDateDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.AnnualVeriRepEndDateForm;

public class AnnualVeriRepEndDateAction extends DispatchAction {

	public ActionForward info(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		request.setAttribute("endDate", dao.find());
		return mapping.findForward("info");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int startYear = 0;
		int endYear = 0;
		SimpleDateFormat df = new SimpleDateFormat("yyyy");
	    int currentYear = Integer.parseInt(df.format(new Date()));
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate a = dao.find();
		if(a != null){
			startYear = Integer.parseInt(a.getAnnualVeriRepEndDateYear());
			endYear = Integer.parseInt(a.getAnnualVeriRepEndDateYear());
		}
		if(startYear == 0 || startYear >= currentYear){
			startYear = currentYear;
		}
		if(startYear == 0 || endYear <= currentYear){
			endYear = currentYear + 1;
		}
		request.setAttribute("startYear", startYear);
		request.setAttribute("endYear", endYear);
		request.setAttribute("endDate", a);
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriRepEndDateForm aForm = (AnnualVeriRepEndDateForm) form;
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate a = dao.find();
		if(a == null)
			a = new AnnualVeriRepEndDate();
		a.setAnnualVeriRepEndDateYear(aForm.getAnnualVeriRepEndDateYear());
		a.setAnnualVeriRepEndDate(aForm.getDateAnnualVeriRepEndDate());
		dao.merge(a);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "AnnualVeriRepEndDate.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()));
		logDao.save(s);
		return mapping.findForward("success");
	}
}